package Service;

import Bean.Page;
import Bean.Ticket;
import Dao.TicketDao;
import Utils.UUIDUtils;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 张
 * Date: 2025-03-10
 * Time: 11:20
 */
//机票业务逻辑实现
public class TicketService {
    private TicketDao ticketDao = new TicketDao();


    //预订机票
    public boolean reservation(String flightId, String ticketType, String seatNumber, double price, String userId) {
        //先根据机票类型给座位号转换成对应值
        switch (ticketType) {
            case "economy":
                seatNumber = "A" + seatNumber;
                break;
            case "business":
                seatNumber = "B" + seatNumber;
                break;
            case "first":
                seatNumber = "C" + seatNumber;
                break;
        }
        Ticket ticket = new Ticket();
        ticket.setTicketId(UUIDUtils.getId());
        ticket.setUserId(userId);
        ticket.setStatus(Ticket.TicketStatus.已预订);
        ticket.setSeatNumber(seatNumber);
        ticket.setPrice(price);
        ticket.setFlightId(flightId);
        System.out.println(ticket.toString());
        return ticketDao.insert(ticket);
    }


    public Page selectByUserId(String userId, int pageNo,int pageSize) {
        Page page = new Page();
        page.setPageNo(pageNo);
        page.setPageSize(pageSize);
        page.setTotalRecords(ticketDao.size(userId));
        page.setList(ticketDao.selectByUserId(userId,pageNo,pageSize));
        return page;
    }

    //查询用户是否购买该航班机票
    public Ticket select(String userId, String flightId,String gqSeatNumber) {
        return ticketDao.select(userId,flightId,gqSeatNumber);
    }

    public int update(Ticket ticket) {
        return ticketDao.update(ticket);
    }

    //约定 2为航班不存在,3为
    public int quit(String userId, String flightNumber, String seatNumber) {
        //先查询用户是否有改航班的订单

        Ticket ticket = ticketDao.select(userId,flightNumber,seatNumber);

        if (ticket == null) {
            return 2;
        }
        //将订单状态修改成退款中
        ticket.setUserId(userId);
        ticket.setStatus(Ticket.TicketStatus.退票中);
        return ticketDao.update(ticket);
    }

    public Page selectQuit(int pageNo, int pageSize, String userId) {
        Page page = new Page();
        page.setPageNo(pageNo);
        page.setPageSize(pageSize);
        Ticket ticket = new Ticket();
        page.setTotalRecords(ticketDao.sizeQuit(userId));
        page.setList(ticketDao.selectQuit(pageNo,pageSize,userId));
        return page;
    }

    public int updateStatus(Ticket ticket) {
        return ticketDao.update(ticket);
    }

    public Page selectQuits(int pageNo, int pageSize) {
        Page page = new Page();
        page.setPageNo(pageNo);
        page.setPageSize(pageSize);
        page.setTotalRecords(ticketDao.sizeQuits());
        page.setList(ticketDao.selectQuits(pageNo,pageSize));
        return page;
    }

    public boolean quitPass(String ticketId) {
        //修改订单信息 status = "已退票" , seat_number = ""
        Ticket ticket = new Ticket();
        ticket.setTicketId(ticketId);
        ticket.setSeatNumber(" ");
        ticket.setStatus(Ticket.TicketStatus.已退票);

        return ticketDao.Pass(ticket);
    }

    public boolean quitRefuse(Ticket ticket) {
        return ticketDao.Refuse(ticket);
    }

    public Ticket selectByTicketId(String ticketId) {
        return ticketDao.selectByTicketId(ticketId);
    }

    public boolean insert(Ticket ticket) {
        return ticketDao.insert(ticket);
    }

    public boolean delece(String ticketId) {
        return ticketDao.delete(ticketId);
    }

    public boolean updateByTicketId(Ticket ticket) {
        return ticketDao.Refuse(ticket);
    }
}
